<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>修改商品</title>
<link rel="stylesheet" href="../layui/css/layui.css">
</head>
<body>

<!-- 添加弹出框的div -->
 <div id="addlayer" style="" >
	<form id="updateForm" class="layui-form" onsubmit="return false" method="post" action="##" enctype="multipart/form-data">
		<!-- 第一行 -->
		<div class="layui-form-item">	  
		    <label class="layui-form-label">商品编号</label>
			<div class="layui-input-inline">
		    	<input type="text" name="goodsId" id="goodsId" th:value="${goods_C.goodsId}" required lay-verify="required" readonly="readonly" autocomplete="off" class="layui-input">
			</div>	
		</div>
		<div class="layui-form-item">		  
		    <label class="layui-form-label">商品名称</label>
			<div class="layui-input-inline">
		    	<input type="text" name="goodsName" id="goodsName" th:value="${goods_C.goodsName}" required lay-verify="required"  autocomplete="off" class="layui-input">
			</div>
		</div>		
		<div class="layui-form-item">
    		<label class="layui-form-label">类型</label>
    		<div class="layui-input-inline">
     		<select name="goodsType" id="goodsType"  lay-verify="required" class="select">
        		<option value="">请选择商品类型</option>           
      		</select>
    		</div>
    	</div> 	
    	<div class="layui-form-item">		  
		    <label class="layui-form-label">单价</label>
			<div class="layui-input-inline">
		    	<input type="text" name="goodsPrice" id="goodsPrice" th:value="${goods_C.goodsPrice}" required lay-verify="required"  autocomplete="off" class="layui-input">
			</div>
		</div>	
		<div class="layui-form-item">		  
		    <label class="layui-form-label">单位</label>
			<div class="layui-input-inline">
		    	<input type="text" name="goodsUnit" id="goodsUnit" th:value="${goods_C.goodsUnit}" required lay-verify="required"  autocomplete="off" class="layui-input">
			</div>
		</div>	
		<div class="layui-form-item">		  
		    <label class="layui-form-label">上传图片</label>
			<div class="layui-input-inline">
		    	<input type="file" name="image" value="上传图片" class="layui-input">
			</div>
		</div>	
		<div class="layui-form-item">		  
		    <label class="layui-form-label">备注</label>
			<div class="layui-input-block">
				<textarea name="goodsNote" id="goodsNote" th:value="${goods_C.goodsNote}" class="layui-textarea"></textarea>
			</div>
		</div>	
		<div class="layui-form-item" style="width:160px;height:40px;margin:auto;">		  
		    <input type="button" id="updatesubmit" class="layui-btn" name="addsubmit" value="确定">
		    <input type="reset"	id="addreset" class="layui-btn layui-btn-primary" name="addreset" value="重置">
		</div>			  		
	</form>
</div>  


<script src="../layui/layui.js"></script>

<script type="text/javascript">
layui.use(['layer','jquery','form','element'],function(){
	var layer=layui.layer;
	var $=layui.jquery;
	var form=layui.form;
	var element=layui.element;
	//var data_A=document.getElementById("updateForm").value;
	//console.log(data_A);
	
	//序列化Form表达的方法
	//定义serializeObject方法，序列化表单
 
	$.fn.serializeObject = function() {
	    var o = {};
	    var a = this.serializeArray();
	    $.each(a, function() {
	        if (o[this.name]) {
	            if (!o[this.name].push) {
	                o[this.name] = [ o[this.name] ];
	            }
	            o[this.name].push(this.value || '');
	        } else {
	            o[this.name] = this.value || '';
	        }
	    });
	    return o;
	};
	layer.ready(function(){
		$.ajax({
			type:'get',
			url:'../goodsType/getTypeList',
			success:function(data){
				$.each(data,function(index,goods){
					$('#goodsType').append(new Option(goods.goodsTypeName));
				});
				form.render("select");
			}
		});		
	});
	$(document).on('click','#updatesubmit',function(){	
		var formData=new FormData($('#updateForm')[0]);
		var goodsName=$("#goodsName").val();
		var goodsType=$("#goodsType option:selected").text();
		var goodsPrice=$("#goodsPrice").val();
		var goodsUnit=$("#goodsUnit").val();
		
		if(goodsName===""){
			layer.msg("商品名称不能为空");
		}
		else if(goodsType==="请选择商品类型"){
			layer.msg("商品类型不能为空");
		}
		else if(goodsPrice===""){
			layer.msg("商品单价不能为空");
		}
		else if(goodsUnit===""){
			layer.msg("商品单位不能为空");
		}	
		else{
			$.ajax({
				type:'post'
				,url:'../goods/doUpdate'
				,data:formData
				,async:false
				,cache:false
				,contentType:false
				,processData:false
				,success:function(){
					var index = parent.layer.getFrameIndex(window.name); 
					parent.layer.close(index);
				}
				,error:function(){
					alert("异常");
				}
			});
		}
	});				
});//layui
</script>
</body>
</html>